package me.leiusual.sharing.ui.account

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import me.leiusual.sharing.api.model.RegisterByQRCode
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.account.model.ParamsRegisterByQRCode
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/8/14
 */
interface AccountRegisterViewModelDelegate {

  val registerByQRCode: LiveData<Data<RegisterByQRCode.Response>>

  fun registerByQRCode(encodedClientId: String, inviteCode: String)

  class Impl @Inject constructor(
      private val userRepository: UserRepository
  ) : AccountRegisterViewModelDelegate {

    private val _registerByQRCode = MutableLiveData<ParamsRegisterByQRCode>()
    override val registerByQRCode: LiveData<Data<RegisterByQRCode.Response>> = Transformations.switchMap(_registerByQRCode) {
      userRepository.registerByQRCode(it).toLiveData()
    }

    override fun registerByQRCode(encodedClientId: String, inviteCode: String) {
      _registerByQRCode.value = ParamsRegisterByQRCode(encodedClientId, inviteCode)
    }
  }
}